package com.yjhealth.libs.core.shapref;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;

import com.yjhealth.libs.core.constants.CoreConstants;
import com.yjhealth.libs.core.core.CoreAppInit;

/**
 * Created by kai.chen on 2017/7/3.
 * one SHARED_NAME,one manager
 */

public class CoreSharpref {
    /*Default*/
    private static final String SHARED_NAME = "hospat_share_core";

    //debug
    private static final String KEY_DEBUG = "debug";
    //network
    public static final String KEY_HTTP_ENVIROMENT = "http_enviroment";
    public static final String KEY_HTTP_API_URL = "http_api_url";
    /*Util*/
    private Context context;

    private static class Holder {
        private static final CoreSharpref INSTANCE = new CoreSharpref(CoreAppInit.getApplication());
    }

    public static CoreSharpref getInstance() {
        return CoreSharpref.Holder.INSTANCE;
    }

    private CoreSharpref(@NonNull Context context) {
        this.context = context.getApplicationContext();
    }

    private SharedPreferences getShaprefrence() {
        return context
                .getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
    }

    private SharedPreferences.Editor getEditor() {
        return context
                .getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE).edit();
    }

    /**
     * @return
     */
    public boolean getDebug() {
        synchronized (this) {
            return getShaprefrence().getBoolean(KEY_DEBUG, CoreConstants.isDebug);
        }
    }

    public boolean setDebug(boolean debug) {
        synchronized (this) {
            return getEditor()
                    .putBoolean(KEY_DEBUG, debug)
                    .commit();
        }
    }

    /**
     * 获取当前网络环境
     *
     * @return
     */
    public String getNetEnviroment(String def) {
        synchronized (this) {
            return getShaprefrence()
                    .getString(KEY_HTTP_ENVIROMENT, def);
        }
    }

    /**
     * 设置当前网络环境
     *
     * @param enviroment
     * @return
     */
    public boolean setNetEnviroment(String enviroment) {
        synchronized (this) {
            return getEditor()
                    .putString(KEY_HTTP_ENVIROMENT, enviroment)
                    .commit();
        }
    }

    /**
     * 获取手动网络url
     *
     * @return
     */
    public String getNetApiUrl(String def) {
        synchronized (this) {
            return getShaprefrence()
                    .getString(KEY_HTTP_API_URL, def);
        }
    }

    /**
     * 设置手动网络url
     *
     * @param url
     * @return
     */
    public boolean setNetApiUrl(String url) {
        synchronized (this) {
            return getEditor()
                    .putString(KEY_HTTP_API_URL, url)
                    .commit();
        }
    }
}
